﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2Logic;

namespace Ras2Win
{
    /// <summary>
    /// Klasinn HitListView - UserControl
    /// Inniheldur eitt listview
    /// </summary>
    public partial class HitListView : UserControl
    {
        #region Member breytur

        #endregion

        #region Smiðir

        /// <summary>
        /// Færibreytulaus smiður
        /// </summary>
        public HitListView()
        {
            InitializeComponent();
        }

        #endregion

        #region Get / Set föll

        #endregion

        #region Private föll

        /// <summary>
        /// Fall sem keyrist þegar viewi er loadað á formi.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HitListView_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                HitListController ctrl = new HitListController();
                foreach (var item in ctrl.GetAllHitlists())
                {
                    m_lstViewHitList.Items.Add(CreateListViewItem(item));
                }
            }
        }

        /// <summary>
        /// Hjálparfall sem smíðar listview item sem eru svo sett inn á listviewið.
        /// </summary>
        /// <param name="hitlist">Tekur inn object af taginu HitList</param>
        /// <returns>Skilar listview itemi</returns>
        private ListViewItem CreateListViewItem(HitList hitlist)
        {
            ListViewItem item = new ListViewItem(Convert.ToString(hitlist.ID));
            item.SubItems.Add(hitlist.Name);

            item.ImageIndex = 0;
            item.Tag = hitlist;
            return item;
        }

        #endregion

        #region Public föll

        /// <summary>
        /// Fall sem editar HitList færslu.
        /// </summary>
        public void OnEditClick()
        {
            if (m_lstViewHitList.SelectedItems.Count == 1)
            {
                ListViewItem listViewItem = m_lstViewHitList.SelectedItems[0];

                HitList hitlist = (HitList)listViewItem.Tag;
                using (EditHitList dlg = new EditHitList())
                {
                    dlg.HitList = hitlist;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        // Sækjum gögnin aftur úr dialog
                        hitlist = dlg.HitList;

                        HitListController ctrl = new HitListController();

                        // Látum vinnslulagið uppfæra nemandann. Ef það
                        // mistekst er kastað villu.
                        ctrl.UpdateHitList(hitlist);

                        //Grípa indexinn.
                        int nIndex = listViewItem.Index;
                        // Við uppfærum færsluna með því að fjarlægja þá sem
                        // er fyrir og setja nýja inn:
                        m_lstViewHitList.Items.Remove(listViewItem);
                        m_lstViewHitList.Items.Insert(nIndex, CreateListViewItem(hitlist));
                    }
                }
            }
        }

        /// <summary>
        /// Fall sem setur inn nýjan HitLista
        /// </summary>
        public void OnNewClick()
        {
            using (EditHitList dlg = new EditHitList())
            {
                dlg.HitList = new HitList();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    HitList hitlist = dlg.HitList;
                    HitListController controller = new HitListController();

                    // TODO: Villuverja.
                    controller.AddHitList(hitlist);
                    m_lstViewHitList.Items.Add(CreateListViewItem(hitlist));
                }
            }
        }

        #endregion

    }
}
